package com.changlu._01单例设计模式;

/**
 * @ClassName lazyTest
 * @Author ChangLu
 * @Date 2022/2/16 18:12
 * @Description 饿汉式(非线程安全的)：双重检测锁
 */
public class LazySingleton {

    //volatile保证不同线程对于公共变量修改可见；禁止指令重排
    private volatile static LazySingleton instance;

    private LazySingleton() {
    }

    //双重判断+syn锁
    public static LazySingleton getInstance(){
        if (instance == null){
            synchronized (LazySingleton.class){
                if (instance == null){
                    instance = new LazySingleton();
                }
            }
        }
        return instance;
    }

    //测试
    public static void main(String[] args) {
        System.out.println(LazySingleton.getInstance());
    }

}